package org.sanguine.tests.tenderpro.sax.actions;

import org.sanguine.tests.tenderpro.sax.actions.Action;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * Author: Alexey Kuznetsov
 * Date: 19.02.14
 * Time: 22:21
 * SVN $Id$
 */
public class ElementCounter extends DefaultHandler implements Action {
    private String element;
    private Map<String, Integer> elemMap = new HashMap<String, Integer>();

    @Override
    public void endDocument() throws SAXException {
        Integer count = elemMap.get(element);
        if (count == null) System.out.printf("%s is not found\n", element);
        else System.out.printf("%s: %d\n", element, count);
    }

    @Override
    public void startElement(String uri, String localName, String qName, Attributes atts) throws SAXException {
        Integer count = elemMap.get(localName);
        if (count == null) elemMap.put(localName, new Integer(1));
        else elemMap.put(localName, ++count);
    }

    @Override
    public void setUp(List<Object> params) {
        if (params != null && params.size() > 1) this.element = ((String)params.get(1)).trim();
        else this.element = "";
    }
}
